package cate.game.fight.test;

import cate.common.table.d.GDHero;
import cate.common.table.fight.skill.row.SkillBaseRow;
import cate.common.table.item.awakenfairy.row.AwakenFairyBaseRow;
import cate.common.table.item.fairy.row.FairyBaseRow;
import cate.common.table.item.favorability.row.FavorabilityAttrRow;
import cate.common.table.item.hero.row.HeroBaseRow;
import cate.common.table.item.kitchenware.row.KitchenwareAttrRow;
import cate.common.table.item.yoke.row.HeroLimitRow;
import cate.game.admin.menu.custom.CustomMenu;
import cate.game.client.msg.MergeMsgSender;
import cate.game.role.Role;
import cate.game.role.bag.awakenfairy.AwakenFairy;
import cate.game.role.bag.equip.Equip;
import cate.game.role.bag.fairy.Fairy;
import cate.game.role.bag.favorability.FavorabilityAttr;
import cate.game.role.bag.furniture.Furniture;
import cate.game.role.bag.gem.Gem;
import cate.game.role.bag.hero.Hero;
import cate.game.role.bag.kitchenware.Kitchenware;
import cate.game.role.bag.newtalent.TalentItem;
import cate.game.role.bag.spice.Spice;

import java.util.ArrayList;
import java.util.List;

public class HeroDevUtil {



	public static void handleTopLevel(Role role,HeroBaseRow heroTpl, Hero hero){
		if (hero.level < heroTpl.levelMax) {
			hero.level = heroTpl.levelMax;
			hero.grade = heroTpl.gradeMax;
		}
	}


	public static void handleTopEquip(Role role, Hero hero) {
		for (int i = 0; i < TOP_EQUIPS.length; i++) {
			Equip equip = new Equip(role, TOP_EQUIPS[i], 1);
			equip.psionicExp = 200;
			equip.gemsOn.doRealOn((byte) 1, new Gem(role, GEMS[i], 1));
			hero.equipsOn.doRealOn((byte) (i + 1), equip);
		}
	}

	public static void handleTopFairy(Role role, HeroBaseRow heroTpl, Hero hero){
		if (heroTpl.star < 11) {
			return;
		}
		//灵器
		Fairy fairy = null;
		for (FairyBaseRow baseRow : role.getGame().table.fairy.base.getList()) {
			if (baseRow.level == 36 && baseRow.heros.contains(heroTpl.protoId)) {
				fairy = new Fairy(role, baseRow.id, 1);
				break;
			}
		}
		if (fairy == null) {
			fairy = new Fairy(role, TOP_COMMON_FAIRY[heroTpl.camp - 1], 1);
		}
		hero.fairiesOn.fairyMap.put((byte) 1, fairy);
	}

	public static void handleTopAwakenFairy(Role role, HeroBaseRow heroTpl, Hero hero) {
		if (heroTpl.star < 12) {
			return;
		}

		List<Integer> taskSkillsProtos = new ArrayList<>();
		for (Integer skillId : heroTpl.taskSkillList) {
			SkillBaseRow baseRow = role.getGame().table.skill.base.get(skillId);
			if (baseRow != null) {
				taskSkillsProtos.add(baseRow.protoId);
			}
		}
		AwakenFairy fairy = null;
		for (AwakenFairyBaseRow baseRow : role.getGame().table.awakenFairy.base.getList()) {
			if (baseRow.quality == 6 && baseRow.level == 35 && taskSkillsProtos.contains(baseRow.activeSkillProtoId)) {
				fairy = new AwakenFairy(role, baseRow.protoId + 35, 1);
				break;
			}
		}
		if (fairy != null) {
			hero.awakenFairiesOn.awakenFairyMap.put((byte) 1, fairy);
		}
	}



	public static void handleTopRune(Role role,HeroBaseRow heroTpl,Hero hero){
		if (heroTpl.star < 7) {
			return;
		}
		//徽章
		hero.runesOn.runeMap.clear();
		switch (heroTpl.job) {
			case GDHero.Job.J1:
				hero.runesOn.doRealOn((byte) 1, CustomMenu.buildTopRune(role, heroTpl,1001904, 1003504));
				hero.runesOn.doRealOn((byte) 2, CustomMenu.buildTopRune(role, heroTpl,1002504, 1001104));
				break;
			case GDHero.Job.J2:
				hero.runesOn.doRealOn((byte) 1, CustomMenu.buildTopRune(role, heroTpl,1001804, 1003604));
				hero.runesOn.doRealOn((byte) 2, CustomMenu.buildTopRune(role, heroTpl,1002504, 1001104));
				break;
			case GDHero.Job.J3:
				hero.runesOn.doRealOn((byte) 1, CustomMenu.buildTopRune(role, heroTpl,1001904, 1003504));
				hero.runesOn.doRealOn((byte) 2, CustomMenu.buildTopRune(role, heroTpl,1002504, 1004404));
				break;
			case GDHero.Job.J4:
				hero.runesOn.doRealOn((byte) 1, CustomMenu.buildTopRune(role, heroTpl,1004704, 1000304));
				hero.runesOn.doRealOn((byte) 2, CustomMenu.buildTopRune(role, heroTpl,1002504, 1004004));
				if (heroTpl.protoId == 5050600) {
					hero.runesOn.doRealOn((byte) 1, CustomMenu.buildTopRune(role, heroTpl,1001804, 1000304));
				}
				break;
			default:
				break;
		}
	}

	public static void handleTopTalent(HeroBaseRow heroTpl, Hero hero) {
		if (heroTpl.star < 13) {
			return;
		}
		//天赋
		hero.newTalentOn.list.clear();
		int[] talents;
		switch (heroTpl.job) {
			case GDHero.Job.J1:
			case GDHero.Job.J2:
				talents = DPS_TALENT;
				break;
			case GDHero.Job.J3:
				talents = J3_TALENT;
				break;
			case GDHero.Job.J4:
			default:
				talents = J4_TALENT;
		}
		for (int i = 0; i < talents.length; i++) {
			hero.newTalentOn.list.add(new TalentItem(talents[i], (byte) i));
		}
	}

	public static void handleTopSpice(Role role, HeroBaseRow heroTpl, Hero hero, MergeMsgSender sender) {
		//星格
		hero.spicesOn.spiceMap.clear();
		if (heroTpl.star < 13) {
			return;
		}
		if (hero.lock.heroContract || hero.lock.heroYoke) {
			return;
		}
		int[] tidArray;
		if (heroTpl.job == GDHero.Job.J4) {
			tidArray = SUPPORT_SPICE;
		} else {
			tidArray = DPS_SPICE;
		}
		for (int i = 0; i < tidArray.length; i++) {
			int tid = tidArray[i];
			Spice spice = new Spice(role, tid, 1);
			spice.level = 10;
			spice.onHeroUid = hero.uid;
			role.getBag().spice.addItem(spice, sender);
			hero.spicesOn.spiceMap.put((byte) (i + 1), spice.uid);
		}
	}

	public static void handleFavor(Role role, HeroBaseRow heroTpl, Hero hero) {
		if (heroTpl.star < 15) {
			return;
		}
		//好感
		for (FavorabilityAttr attr : hero.favorabilityOn.favorabilitysAttrs) {
			FavorabilityAttrRow attrRow = role.getGame().table.favorability.attr.get(attr.attrId);
			if (attrRow != null) {
				attr.times.clear();
				attr.value = 0;
				int time = 135;
				if (attr.attrId == 3) {
					time = 30;
				}
				attr.addValue(207016, time, attrRow.addValue * time);
			}
		}
	}

	public static void handleFurniture(Role role, HeroBaseRow heroTpl, Hero hero) {
		if (heroTpl.star < 13) {
			return;
		}
		for (Byte pos : T1_POS) {
			hero.furnituresOn.furnitureMap.put(pos, topFurniture(role, heroTpl, T1_TID));
		}
		for (Byte pos : T2_POS) {
			hero.furnituresOn.furnitureMap.put(pos, topFurniture(role, heroTpl, T2_TID));
		}
		for (Byte pos : T3_POS) {
			hero.furnituresOn.furnitureMap.put(pos, topFurniture(role, heroTpl, T3_TID));
		}
		for (Byte pos : T4_POS) {
			hero.furnituresOn.furnitureMap.put(pos, topFurniture(role, heroTpl, T4_TIDS[heroTpl.job - 1]));
		}
	}

	public static void handleYoke(Role role, HeroBaseRow heroTpl, Hero hero, MergeMsgSender sender) {
		if (heroTpl.star < 15) {
			return;
		}
		HeroLimitRow limitRow = role.getGame().table.yoke.heroLimit.getRowByProtoId(heroTpl.protoId);
		if (limitRow == null) {
			return;
		}
		if (!hero.yokeOn.heroUidMap.containsKey((byte) 1)) {
			Hero yoke1 = buildYokeHero(role, YOKE_CAMP_HERO[limitRow.l1.limit - 1], sender);
			hero.yokeOn.heroUidMap.put((byte) 1, yoke1.uid);
		}
		if (!hero.yokeOn.heroUidMap.containsKey((byte) 2)) {
			Hero yoke2 = buildYokeHero(role, limitRow.l2.limit + 90010, sender);
			hero.yokeOn.heroUidMap.put((byte) 2, yoke2.uid);
		}
	}

	public static Hero buildYokeHero(Role role, int heroTid, MergeMsgSender sender) {
		Hero yokeHero = new Hero(role, heroTid);
		yokeHero.level = 400;
		yokeHero.grade = 6;
		yokeHero.lock.heroYoke = true;
		handleTopEquip(role, yokeHero);
		role.getBag().hero.addItem(yokeHero, sender);
		return yokeHero;
	}



	public static Furniture topFurniture(Role role, HeroBaseRow heroTpl, int tid) {
		Furniture furniture = new Furniture(role, tid);
		furniture.job = heroTpl.job;
		furniture.camp = heroTpl.camp;
		switch (furniture.getTpl(role.getGame()).type) {
			case 2:
			case 3: furniture.level = 3;break;
			case 4: furniture.level = 8;break;
			default: break;
		}
		switch (tid){
			case T1_TID:
				furniture.camp1AttrId = 11012;
				break;
			case T2_TID:
			case T3_TID:
				furniture.camp1AttrId = 11022;
				switch (heroTpl.job){
					case GDHero.Job.J1:
						furniture.job1AttrId = 14103;
						furniture.job2AttrId = 76101;
						break;
					case GDHero.Job.J2:
						furniture.job1AttrId = 14109;
						furniture.job2AttrId = 76107;
						break;
					case GDHero.Job.J3:
						furniture.job1AttrId = 14115;
						furniture.job2AttrId = 76113;
						break;
					case GDHero.Job.J4:
						furniture.job1AttrId = 14122;
						furniture.job2AttrId = 76119;
						break;
					default:break;
				}
				break;
			default:
				furniture.camp1AttrId = 11032;
				switch (heroTpl.job){
					case GDHero.Job.J1:
						furniture.job1AttrId = 75103;
						furniture.job2AttrId = 76201;
						break;
					case GDHero.Job.J2:
						furniture.job1AttrId = 75109;
						furniture.job2AttrId = 76207;
						break;
					case GDHero.Job.J3:
						furniture.job1AttrId = 75115;
						furniture.job2AttrId = 76213;
						break;
					case GDHero.Job.J4:
						furniture.job1AttrId = 75122;
						furniture.job2AttrId = 76219;
						break;
					default:break;
				}
				break;
		}
		return furniture;
	}

	public static void handleKitchenware(Role role, HeroBaseRow heroTpl, Hero hero) {
		hero.kitchenwaresOn.kitchenwareMap.clear();
		for (byte i = 1; i <= 4; i++) {
			hero.kitchenwaresOn.kitchenwareMap.put(i, topKitchenware(role, heroTpl, i));
		}
	}

	public static Kitchenware topKitchenware(Role role, HeroBaseRow heroTpl, byte pos) {
		int tid = TOP_KITCHEN_WARE[pos - 1];
		Kitchenware kitchenware = new Kitchenware(role, tid, 1);
		kitchenware.maxLevel = 20;
		kitchenware.level = 20;
		kitchenware.attrIds.clear();

		List<Integer> attrGroups = role.getGame().table.kitchenware.levelUp.findRow(tid, 20).attrGroup;
		for (int i = 0; i < attrGroups.size(); i++) {
			int attrGroup = attrGroups.get(i);
			List<Integer> attrIds = new ArrayList<>();
			int tarIndex = 0;
			for (KitchenwareAttrRow row : role.getGame().table.kitchenware.attr.getList()) {
				if (row.attrGroup == attrGroup) {
					attrIds.add(row.id);
				}
			}
			switch (i) {
				case 0:
				case 3:
					tarIndex = heroTpl.camp - 1;
					if (heroTpl.camp == 5) {
						tarIndex = heroTpl.camp;
					}
					if (heroTpl.camp == 6) {
						tarIndex = heroTpl.camp - 2;
					}
					break;
				case 2:
					if (pos == 1 || pos == 3) {
						if (heroTpl.job <= 2) {
							tarIndex = 0;
						} else {
							tarIndex = 5;
						}
					} else {
						if (heroTpl.job <= 2) {
							tarIndex = 5;
						} else {
							tarIndex = 4;
						}
					}
					break;
				default:
					break;
			}
			kitchenware.attrIds.add(attrIds.get(tarIndex));
		}

		kitchenware.skillIds.clear();
		int[] skillArray = null;
		switch (heroTpl.job) {
			case GDHero.Job.J1:
				skillArray = J1_KITCHEN_SKILL;
				break;
			case GDHero.Job.J2:
				skillArray = J2_KITCHEN_SKILL;
				break;
			case GDHero.Job.J3:
				skillArray = J3_KITCHEN_SKILL;
				break;
			case GDHero.Job.J4:
				skillArray = J4_KITCHEN_SKILL;
				break;
			default:
				break;
		}
		if (skillArray != null) {
			kitchenware.skillIds.add(skillArray[pos - 1]);
		}
		return kitchenware;
	}

	private static final byte[] T4_POS = {0, 2, 4, 14, 16, 18};
	private static final byte[] T3_POS = {28, 30, 32};
	private static final byte[] T2_POS = {6, 20};
	private static final byte[] T1_POS = {34};

	private static final int T1_TID = 10110010;
	private static final int T2_TID = 10210010;
	private static final int T3_TID = 10220010;
	private static final int[] T4_TIDS = {10410010,10420010,10440010,10430010};


	private static final int[] TOP_EQUIPS = {10503016,10503026,10503036,10503046};

	private static final int[] GEMS = {133605,130006,131006,132006};

	private static final int[] TOP_COMMON_FAIRY = {10300135,10300235,10300335,10300535,10300735,10300635};

	private static final int[] DPS_SPICE = {11305,11505,11705,11905,12505,12605};
	private static final int[] SUPPORT_SPICE = {11405,11605,11805,12005,12505,12605};

	private static final int[] DPS_TALENT = {1003004, 1003304, 1004204};
	private static final int[] J3_TALENT = {1000404, 1003304, 1004204};
	private static final int[] J4_TALENT = {1004504, 1003304, 1004204};

	private static final int[] YOKE_CAMP_HERO = {1141310,2141210,3140610,4141110,5140910,6140310};

	private static final int[] TOP_KITCHEN_WARE = {301301,302301,303301,304301};

	private static final int[] J1_KITCHEN_SKILL = {3, 9, 18, 27};
	private static final int[] J2_KITCHEN_SKILL = {3, 9, 18, 27};
	private static final int[] J3_KITCHEN_SKILL = {24, 57, 66, 72};
	private static final int[] J4_KITCHEN_SKILL = {24, 57, 66, 72};
}
